/**CreateCategoryLib.js v.0.0.6******************************************
 * H vivliothikh ayth xrhsimeuei gia thn diaxeirish twn attributes
 * kata thn dhmioyrgia kathgorias. Yparxoun oi synarthseis gia thn
 * katallhlh emfanish twn stoixeiwn pou prepei na eisagei o xrhshs
 * kathws kai oi synarthseis gia thn katallhlh diaxeirish twn dedo-
 * menwn twn attributes pou eisagei o xrhsths
 * Author: pandiam
 ************************************************************************/
$.ajaxSetup({ cache: false });
var ajax_load = "<img src='images/load.gif' alt='loading...' />";
$("#profileSideBar").load($("#profileSideBar").html(ajax_load).load('profileSideBar.php'));

Array.prototype.remove = function (from, to)
{
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

function convert(argument)
{
    if (argument == "") return;
    var lame = argument.split(",");
    for (var j = 0; j < lame.length; j++)
    {
        obj.push(lame[j]);
    }
}

function toggle(argument)
{
    if (argument == 2) document.getElementById("hide_this").style.display = 'none';
    else document.getElementById("hide_this").style.display = '';
}

function toggleSelect()
{
    if (document.getElementById('best').selectedIndex == 2) document.getElementById('close_to').style.visibility = 'visible';
    else document.getElementById('close_to').style.visibility = 'hidden';
}
var obj;
if (obj == null) obj = new Array();

function deleteAttr(argument)
{
    obj.remove(argument);
    changeContent("delete");
}

function keepAttribute()
{
    var name = document.getElementById('attrname');
    var button1 = null;
    var best_val = null;
    var close_to = null;
    var checkString1 = "";
    var checkString2 = "";
    var checkString3 = "";
    if (document.createCategory.type[0].checked) button1 = document.createCategory.type[0].value;
    else if (document.createCategory.type[1].checked) button1 = document.createCategory.type[1].value;
    else button1 = document.createCategory.type[2].value;
    //na rwthsw gia to measurable kai to unmeasurable
    if (button1 == "measurable")
    {
        best_val = document.getElementById('best');
        if (best_val.value == "close_to")
        {
            close_to = document.getElementById('close_to');
            //edw mporei na xreiastoume elegxo gia to close_to
            checkString1 = close_to.value;
        }
        checkString2 = best_val.value + "~" + checkString1;
    }
    if (name.value.length == 0) alert("Attribute name is not completed"); //TODO change alert
    else if (checkDoubleData(name.value) == false) alert("You have already entered an attribute with the given name"); //TODO change alert
    else if (checkNameValidation(name.value) == false) alert("The given name is not in the right form. The name must not contain the following characters : + * & ^ % $ # @ ! ~ ` > < ? / \ \" '"); //TODO change alert
    else
    {
        checkString3 = name.value + "~" + button1 + "~" + checkString2;
        obj.push(checkString3);
        changeContent(checkString3);
        document.getElementById('attrname').value = "";
        document.createCategory.type[0].checked = true;
        toggle(1);
        document.getElementById('best').value = "min";
        document.getElementById('best').selectedIndex = 0;
        document.getElementById('close_to').value = "";
        document.getElementById('close_to').style.visibility = 'hidden';
    }
}

//na koitaxw na ftiaxw ligo tous elegxous kai thn ektypwsh!!
function checkNameValidation(name)
{
    var regex = /^[0-9A-Za-z][0-9A-Za-z_\s-]+$/;
    if (!regex.test(name)) return false;
    return true;
}

function checkDoubleData(name)
{
    for (var i = 0; i < obj.length; i++)
    {
        var split = obj[i].split("~");
        if (split[0].toLowerCase() == name.toLowerCase()) return false;
    }
    return true;
}

function changeContent(argument)
{
    var deleteLik;
    if (argument != "delete")
    {
        deleteLik = "<a href=\"javascript:deleteAttr( " + (obj.length - 1) + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
        $("#attrCat").html($("#attrCat").html() + argument.replace(/~/gi, " ") + " " + deleteLik + " " + "<br/>");
    }
    else
    {
        var htmlCode = "";
        for (var i = 0; i < obj.length; i++)
        {
            deleteLik = "<a href=\"javascript:deleteAttr( " + i + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
            htmlCode = htmlCode + obj[i].replace(/~/gi, " ") + " " + deleteLik + " " + "<br/>";
        }
        $("#attrCat").html(htmlCode);
    }
}

//elelgxoume gia ola ta dedomena pou exei eisagei o xrhsths kai kanoume redirect
//sthn katallhlh selida an ola einai kala
function passData()
{
    if (document.getElementById('catname').value.length == 0) alert("Category name is not completed") //TODO change alert
    else if (obj.length == 0) alert("You must insert at least 1 attribute to this category"); //TODO change alert
    else
    {
        document.createCategory.data.value = obj;
        document.createCategory.submit();
    }
}